<template>
  <transition name="fade">
    <div v-if="visible" class="loading-progress-overlay">
      <div class="loading-progress-container">
        <div class="loading-icon">
          <div class="spinner"></div>
        </div>
        
        <div class="progress-info">
          <div class="progress-text">
            正在加载数据...
          </div>
          <div class="progress-detail">
            {{ completed }} / {{ total }}
          </div>
          <!-- 显示当前正在加载的任务 -->
          <div class="current-tasks" v-if="currentTasks.length > 0">
            <div class="task-item" v-for="(task, index) in currentTasks" :key="index">
              <span class="task-icon">⚡</span>
              <span class="task-name">{{ task }}</span>
            </div>
          </div>
        </div>
        
        <div class="progress-bar-container">
          <div 
            class="progress-bar" 
            :style="{ width: percent + '%' }"
          ></div>
        </div>
        
        <div class="progress-percent">
          {{ percent }}%
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import requestQueue from '../utils/requestQueue.js'

const visible = ref(false)
const completed = ref(0)
const total = ref(0)
const percent = ref(0)
const currentTasks = ref([])

let hideTimer = null

// 进度更新回调
const handleProgress = (comp, tot, perc, tasks) => {
  completed.value = comp
  total.value = tot
  percent.value = perc
  currentTasks.value = tasks || []
  
  // 显示进度条
  if (tot > 0 && comp < tot) {
    visible.value = true
    clearTimeout(hideTimer)
  }
  
  // 完成后延迟隐藏
  if (comp >= tot && tot > 0) {
    hideTimer = setTimeout(() => {
      visible.value = false
      // 延迟重置，等待淡出动画
      setTimeout(() => {
        requestQueue.reset()
      }, 500)
    }, 800)
  }
}

onMounted(() => {
  requestQueue.onProgress(handleProgress)
})

onUnmounted(() => {
  clearTimeout(hideTimer)
})
</script>

<style scoped>
.loading-progress-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  backdrop-filter: blur(4px);
}

.loading-progress-container {
  background: linear-gradient(135deg, rgba(28, 35, 64, 0.95), rgba(18, 25, 54, 0.95));
  border: 1px solid rgba(0, 186, 255, 0.3);
  border-radius: 12px;
  padding: 40px 50px;
  min-width: 400px;
  box-shadow: 0 8px 32px rgba(0, 186, 255, 0.2),
              0 0 80px rgba(0, 186, 255, 0.1),
              inset 0 0 40px rgba(0, 186, 255, 0.05);
  text-align: center;
}

.loading-icon {
  margin-bottom: 25px;
}

.spinner {
  width: 60px;
  height: 60px;
  margin: 0 auto;
  border: 4px solid rgba(0, 186, 255, 0.2);
  border-top: 4px solid #00baff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.progress-info {
  margin-bottom: 20px;
}

.progress-text {
  font-size: 18px;
  color: #00baff;
  margin-bottom: 8px;
  font-weight: 500;
  text-shadow: 0 0 10px rgba(0, 186, 255, 0.5);
}

.progress-detail {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.6);
  font-family: 'Monaco', 'Consolas', monospace;
}

.progress-bar-container {
  width: 100%;
  height: 8px;
  background: rgba(0, 186, 255, 0.1);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 15px;
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.3);
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, 
    #00baff 0%, 
    #00e4ff 50%, 
    #00baff 100%);
  border-radius: 4px;
  transition: width 0.3s ease;
  box-shadow: 0 0 10px rgba(0, 186, 255, 0.6),
              0 0 20px rgba(0, 186, 255, 0.4);
  position: relative;
  overflow: hidden;
}

.progress-bar::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg,
    transparent,
    rgba(255, 255, 255, 0.3),
    transparent);
  animation: shimmer 2s infinite;
}

@keyframes shimmer {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

.progress-percent {
  font-size: 24px;
  font-weight: bold;
  color: #00baff;
  font-family: 'Monaco', 'Consolas', monospace;
  text-shadow: 0 0 15px rgba(0, 186, 255, 0.8);
}

/* 当前任务列表 */
.current-tasks {
  margin-top: 20px;
  padding-top: 15px;
  border-top: 1px solid rgba(0, 186, 255, 0.2);
  max-height: 100px;
  overflow-y: auto;
}

.task-item {
  display: flex;
  align-items: center;
  padding: 6px 0;
  color: rgba(255, 255, 255, 0.8);
  font-size: 13px;
  animation: fadeInLeft 0.3s ease;
}

.task-icon {
  margin-right: 8px;
  font-size: 14px;
  animation: pulse 1.5s ease-in-out infinite;
}

.task-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@keyframes fadeInLeft {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.6;
    transform: scale(1.2);
  }
}

/* 自定义滚动条 */
.current-tasks::-webkit-scrollbar {
  width: 4px;
}

.current-tasks::-webkit-scrollbar-track {
  background: rgba(0, 186, 255, 0.1);
  border-radius: 2px;
}

.current-tasks::-webkit-scrollbar-thumb {
  background: rgba(0, 186, 255, 0.5);
  border-radius: 2px;
}

.current-tasks::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 186, 255, 0.7);
}

/* 淡入淡出动画 */
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from, .fade-leave-to {
  opacity: 0;
}

/* 响应式 */
@media (max-width: 768px) {
  .loading-progress-container {
    min-width: 300px;
    padding: 30px 40px;
  }
  
  .progress-text {
    font-size: 16px;
  }
  
  .progress-percent {
    font-size: 20px;
  }
}
</style>

